package app.android.kdictionary.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import app.android.kdictionary.R;
import app.android.kdictionary.R.id;
import app.android.kdictionary.Component.MainMultiple;
import app.android.kdictionary.Component.MainSpecify;
import app.android.kdictionary.adapter.SearchFormAdapter;
import app.android.kdictionary.adapter.WordClassAdapter;
import app.android.kdictionary.data.SearchFormData;
import app.android.kdictionary.data.WordClassData;
import app.android.kdictionary.enums.SearchType;

public class DetailActivity extends DefaultActivity {
	private LinearLayout linearLayout;
	
	private ImageButton multipleButton;
	private ImageButton specifyButton;
	private ImageButton plusButton;
	private ImageButton minusButton;
	private ImageButton searchButton;
	
	private ListView listView01;
	private ListView listView02;
	
	private ImageButton button01;
	private ImageButton button02;
	private ImageButton button03;
	private ImageButton button04;
	private ImageButton button05;
	private ImageButton button06;
	private ImageButton button07;
	private ImageButton button08;
	private ImageButton button09;
	private ImageButton button10;
	private ImageButton button11;
	private ImageButton button12;
	private ImageButton button13;
	private ImageButton button14;
	private ImageButton rangeButton; 
	private ImageButton jasoButton;
	
	private EditText startEditText;
	private EditText endEditText;
	
	private EditText edit01;
	private EditText edit02;
	private EditText edit03;
	private EditText edit04;
	private EditText edit05;
	private EditText edit06;
	private EditText edit07;
	private EditText edit08;
	private EditText edit09;
	private EditText edit10;
	private EditText edit11;
	private EditText edit12;
	
	private SearchFormAdapter mSearchFormAdapter;
	private WordClassAdapter mWordClassAdapter;
	
	private static final int ITEM_01 = 1;
	private static final int ITEM_02 = 2;
	private static final int ITEM_03 = 3;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.detail);
        
		findView();
		setListener();
		initMultipleTask();
    }
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    	switch(resultCode) {
    		case RESULT_OK : {
    			SearchFormData searchFormData = null;
    			switch(requestCode) {
    				case ITEM_01 : searchFormData = (SearchFormData) mSearchFormAdapter.getItem(0); break;
    				case ITEM_02 : searchFormData = (SearchFormData) mSearchFormAdapter.getItem(1); break;
    				case ITEM_03 : searchFormData = (SearchFormData) mSearchFormAdapter.getItem(2); break;
    			}
    			
    			searchFormData.setPicker1(data.getIntExtra("picker1", -1));
    			searchFormData.setPicker1_o(data.getIntExtra("picker1_o", -1));
				searchFormData.setPicker2(data.getIntExtra("picker2", -1));
				searchFormData.setText(data.getStringExtra("text"));
				
				mSearchFormAdapter.notifyDataSetChanged();
    			break;
    		}
    		
    		case RESULT_CANCELED : {
				break;
    		}
    	}
		super.onActivityResult(requestCode, resultCode, data);
    }
    
    private void findView() {
    	linearLayout = (LinearLayout)findViewById(id.detail_main);
    	multipleButton = (ImageButton)findViewById(id.title_02_tab_multiple);
		specifyButton = (ImageButton)findViewById(id.title_02_tab_specify);
		plusButton = (ImageButton)findViewById(id.title_02_plus);
		minusButton = (ImageButton)findViewById(id.title_02_minus);
		searchButton = (ImageButton)findViewById(id.title_02_search);
    }
    
    private void setListener() {
		multipleButton.setOnClickListener(mMultipleClickListener);
		specifyButton.setOnClickListener(mSpecifyClickListener);
		plusButton.setOnClickListener(mPlusClickListener);
		minusButton.setOnClickListener(mMinusClickListener);
		searchButton.setOnClickListener(mSearchClickListener);
	}
    
    private void initMultipleTask() {
    	// remove
    	linearLayout.removeAllViewsInLayout();
    	
    	// create & add
		MainMultiple mainMultiple = new MainMultiple(getBaseContext());
		LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT);
		linearLayout.addView(mainMultiple, lp);
		
		// set listview
    	listView01 = (ListView)findViewById(id.multiple_list_01);
    	listView02 = (ListView)findViewById(id.multiple_list_02);

    	listView01.setOnItemClickListener(mSearchFormItemClickListener);
    	listView02.setOnItemClickListener(mWordClassItemClickListener);

    	mSearchFormAdapter = new SearchFormAdapter(getLayoutInflater(), getBaseContext());
    	mSearchFormAdapter.addItem(new SearchFormData(0, -1, 0, ""));
    	listView01.setAdapter(mSearchFormAdapter);
 
		// 메뉴 리스트
		String[] strArr = getResources().getStringArray(R.array.word_class);
		mWordClassAdapter = new WordClassAdapter(getLayoutInflater());
		for(int i=0; i<strArr.length; i++) {
			mWordClassAdapter.addItem(new WordClassData(false, strArr[i]));
		}
		listView02.setAdapter(mWordClassAdapter);
    }
	
	private OnClickListener mMultipleClickListener = new OnClickListener() {
	    public void onClick(View v) {
	    	// detail main
			initMultipleTask();
			
			// plus minus search button enable
			plusButton.setVisibility(View.VISIBLE);
			minusButton.setVisibility(View.VISIBLE);
			searchButton.setVisibility(View.VISIBLE);
	    }
	};
	
	private OnClickListener mSpecifyClickListener = new OnClickListener() {
	    public void onClick(View v) {
	    	// detail main
	    	linearLayout.removeAllViewsInLayout();
			
			MainSpecify mainSpecify = new MainSpecify(getBaseContext());
			LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT);
			linearLayout.addView(mainSpecify, lp);
	    	
			// 자모별 찾기
			button01 = (ImageButton)findViewById(id.main_specify_1_button_01);
			button02 = (ImageButton)findViewById(id.main_specify_1_button_02);
			button03 = (ImageButton)findViewById(id.main_specify_1_button_03);
			button04 = (ImageButton)findViewById(id.main_specify_1_button_04);
			button05 = (ImageButton)findViewById(id.main_specify_1_button_05);
			button06 = (ImageButton)findViewById(id.main_specify_1_button_06);
			button07 = (ImageButton)findViewById(id.main_specify_1_button_07);
			button08 = (ImageButton)findViewById(id.main_specify_1_button_08);
			button09 = (ImageButton)findViewById(id.main_specify_1_button_09);
			button10 = (ImageButton)findViewById(id.main_specify_1_button_10);
			button11 = (ImageButton)findViewById(id.main_specify_1_button_11);
			button12 = (ImageButton)findViewById(id.main_specify_1_button_12);
			button13 = (ImageButton)findViewById(id.main_specify_1_button_13);
			button14 = (ImageButton)findViewById(id.main_specify_1_button_14);
			
			button01.setOnClickListener(mButton01ClickListener);
			button02.setOnClickListener(mButton02ClickListener);
			button03.setOnClickListener(mButton03ClickListener);
			button04.setOnClickListener(mButton04ClickListener);
			button05.setOnClickListener(mButton05ClickListener);
			button06.setOnClickListener(mButton06ClickListener);
			button07.setOnClickListener(mButton07ClickListener);
			button08.setOnClickListener(mButton08ClickListener);
			button09.setOnClickListener(mButton09ClickListener);
			button10.setOnClickListener(mButton10ClickListener);
			button11.setOnClickListener(mButton11ClickListener);
			button12.setOnClickListener(mButton12ClickListener);
			button13.setOnClickListener(mButton13ClickListener);
			button14.setOnClickListener(mButton14ClickListener);
			
			// 범위로 찾기
			startEditText = (EditText)findViewById(id.specify_2_edit01);
			endEditText = (EditText)findViewById(id.specify_2_edit02);
			rangeButton = (ImageButton)findViewById(id.main_specify_2_button);
			rangeButton.setOnClickListener(mRangeButtonClickListener);
			
			// 자소로 찾기
			edit01 = (EditText)findViewById(id.main_specify_3_edit_01);
			edit02 = (EditText)findViewById(id.main_specify_3_edit_02);
			edit03 = (EditText)findViewById(id.main_specify_3_edit_03);
			edit04 = (EditText)findViewById(id.main_specify_3_edit_04);
			edit05 = (EditText)findViewById(id.main_specify_3_edit_05);
			edit06 = (EditText)findViewById(id.main_specify_3_edit_06);
			edit07 = (EditText)findViewById(id.main_specify_3_edit_07);
			edit08 = (EditText)findViewById(id.main_specify_3_edit_08);
			edit09 = (EditText)findViewById(id.main_specify_3_edit_09);
			edit10 = (EditText)findViewById(id.main_specify_3_edit_10);
			edit11 = (EditText)findViewById(id.main_specify_3_edit_11);
			edit12 = (EditText)findViewById(id.main_specify_3_edit_12);
			
			jasoButton = (ImageButton)findViewById(id.main_specify_3_button);
			jasoButton.setOnClickListener(mJasoButtonClickListener);
			
			// plus minus search button disable
			plusButton.setVisibility(View.INVISIBLE);
			minusButton.setVisibility(View.INVISIBLE);
			searchButton.setVisibility(View.INVISIBLE);
	    }
	};
	
	private OnClickListener mPlusClickListener = new OnClickListener() {
	    public void onClick(View v) {
	    	mSearchFormAdapter.addItem(new SearchFormData(0, -1, 0, ""));
			
			listView01.setAdapter(mSearchFormAdapter);
			listView02.setAdapter(mWordClassAdapter);
	    }
	};
	
	private OnClickListener mMinusClickListener = new OnClickListener() {
	    public void onClick(View v) {
			mSearchFormAdapter.removeLastItem();
			
			listView01.setAdapter(mSearchFormAdapter);
			listView02.setAdapter(mWordClassAdapter);
	    }
	};
	
	private OnClickListener mSearchClickListener = new OnClickListener() {
	    public void onClick(View v) {

	    	String[] strPickerValue1 = getResources().getStringArray(R.array.picker_list_1_value);
			String[] strPickerOptionValue1 = getResources().getStringArray(R.array.picker_option_list_1_value);
			int[] strPickerOptionValue2 = getResources().getIntArray(R.array.picker_option_list_2_value);
			int[] strWordClassValue = getResources().getIntArray(R.array.word_class_value);
			
			String searchText[] = new String[mSearchFormAdapter.getCount()];
			String table[] = new String[mSearchFormAdapter.getCount()];
			String gubun[] = new String[mSearchFormAdapter.getCount()];
			String orgLang[] = new String[mSearchFormAdapter.getCount()];
			String techTerm[] = new String[mSearchFormAdapter.getCount()];
			String spCode[] = new String[mWordClassAdapter.getCount()];
			
			for( int i=0; i<mSearchFormAdapter.getCount(); i++ ) {
				SearchFormData data  = mSearchFormAdapter.getItem(i);
				Log.i("SearchFormData", data.getPicker1() + "/" +data.getPicker1_o() + "/" + data.getPicker2() + "/" + data.getText());
				
				searchText[i] = ""+data.getText();
				table[i] = ""+strPickerValue1[data.getPicker1()];
				gubun[i] = ""+String.valueOf(data.getPicker2());
				if( data.getPicker1()==1 ) {
					orgLang[i] = strPickerOptionValue1[data.getPicker1_o()];
					techTerm[i] = "";
				}
				else if( data.getPicker1()==13 ){
					orgLang[i] = "";
					techTerm[i] = ""+String.valueOf(strPickerOptionValue2[data.getPicker1_o()]);
				}
				else {
					orgLang[i] = "";
					techTerm[i] = "";
				}
			}
			
			
			for(int j=0; j<mWordClassAdapter.getCount(); j++ ) {
				
				if( mWordClassAdapter.getItem(j).isCheck() ) {
					spCode[j] = String.valueOf(strWordClassValue[j]);
				}
				else {
					spCode[j] = "";
				}
			}

			Intent intent = new Intent(DetailActivity.this, SearchResultActivity.class);
			intent.putExtra("searchType", SearchType.MULTI_SEARCH.name());
			intent.putExtra("searchText", searchText);
			intent.putExtra("table", table);
			intent.putExtra("gubun", gubun);
			intent.putExtra("orgLang", orgLang);
			intent.putExtra("techTerm", techTerm);
			intent.putExtra("spCode", spCode);

			startActivity(intent);
			
			
			finish();
	    }
	};
	

	private AdapterView.OnItemClickListener mSearchFormItemClickListener = new AdapterView.OnItemClickListener() {
		@Override
		public void onItemClick(AdapterView<?> arg0, View view, int position, long id) {
			Intent startIntent  = new Intent(getBaseContext(), PickerActivity.class);
			startIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			switch(position) {
				case 0 :
					startActivityForResult(startIntent, ITEM_01);
					break;
				case 1 :
					startActivityForResult(startIntent, ITEM_02);
					break;
				case 2 :
					startActivityForResult(startIntent, ITEM_03);
					break;
			}
		}
	};
	
	private AdapterView.OnItemClickListener mWordClassItemClickListener = new AdapterView.OnItemClickListener() {
		@Override
		public void onItemClick(AdapterView<?> arg0, View view, int position, long id) {
			if( mWordClassAdapter.getItem(position).isCheck() ) {
				mWordClassAdapter.getItem(position).setCheck(false);
			}
			else {
				mWordClassAdapter.getItem(position).setCheck(true);
			}
			mWordClassAdapter.notifyDataSetChanged();
		}
	};
	
	private OnClickListener mButton01ClickListener = new OnClickListener() {
	    public void onClick(View v) {
	    	Intent intent = new Intent(DetailActivity.this, SearchResultActivity.class);
	    	intent.putExtra("searchType", SearchType.INDEX_SEARCH.name());
			intent.putExtra("jamo", "ㄱ");
			startActivity(intent);
	    }
	};
	
	private OnClickListener mButton02ClickListener = new OnClickListener() {
	    public void onClick(View v) {
	    	Intent intent = new Intent(DetailActivity.this, SearchResultActivity.class);
	    	intent.putExtra("searchType", SearchType.INDEX_SEARCH.name());
			intent.putExtra("jamo", "ㄴ");
			startActivity(intent);
	    }
	};
	
	private OnClickListener mButton03ClickListener = new OnClickListener() {
	    public void onClick(View v) {
	    	Intent intent = new Intent(DetailActivity.this, SearchResultActivity.class);
	    	intent.putExtra("searchType", SearchType.INDEX_SEARCH.name());
			intent.putExtra("jamo", "ㄷ");
			startActivity(intent);
	    }
	};
	
	private OnClickListener mButton04ClickListener = new OnClickListener() {
	    public void onClick(View v) {
	    	Intent intent = new Intent(DetailActivity.this, SearchResultActivity.class);
	    	intent.putExtra("searchType", SearchType.INDEX_SEARCH.name());
			intent.putExtra("jamo", "ㄹ");
			startActivity(intent);
	    }
	};
	
	private OnClickListener mButton05ClickListener = new OnClickListener() {
	    public void onClick(View v) {
	    	Intent intent = new Intent(DetailActivity.this, SearchResultActivity.class);
	    	intent.putExtra("searchType", SearchType.INDEX_SEARCH.name());
			intent.putExtra("jamo", "ㅁ");
			startActivity(intent);
	    }
	};
	
	private OnClickListener mButton06ClickListener = new OnClickListener() {
	    public void onClick(View v) {
	    	Intent intent = new Intent(DetailActivity.this, SearchResultActivity.class);
	    	intent.putExtra("searchType", SearchType.INDEX_SEARCH.name());
			intent.putExtra("jamo", "ㅂ");
			startActivity(intent);
	    }
	};
	
	private OnClickListener mButton07ClickListener = new OnClickListener() {
	    public void onClick(View v) {
	    	Intent intent = new Intent(DetailActivity.this, SearchResultActivity.class);
	    	intent.putExtra("searchType", SearchType.INDEX_SEARCH.name());
			intent.putExtra("jamo", "ㅅ");
			startActivity(intent);
	    }
	};
	
	private OnClickListener mButton08ClickListener = new OnClickListener() {
	    public void onClick(View v) {
	    	Intent intent = new Intent(DetailActivity.this, SearchResultActivity.class);
	    	intent.putExtra("searchType", SearchType.INDEX_SEARCH.name());
			intent.putExtra("jamo", "ㅇ");
			startActivity(intent);
	    }
	};
	
	private OnClickListener mButton09ClickListener = new OnClickListener() {
	    public void onClick(View v) {
	    	Intent intent = new Intent(DetailActivity.this, SearchResultActivity.class);
	    	intent.putExtra("searchType", SearchType.INDEX_SEARCH.name());
			intent.putExtra("jamo", "ㅈ");
			startActivity(intent);
	    }
	};
	
	private OnClickListener mButton10ClickListener = new OnClickListener() {
	    public void onClick(View v) {
	    	Intent intent = new Intent(DetailActivity.this, SearchResultActivity.class);
	    	intent.putExtra("searchType", SearchType.INDEX_SEARCH.name());
			intent.putExtra("jamo", "ㅊ");
			startActivity(intent);
	    }
	};
	
	private OnClickListener mButton11ClickListener = new OnClickListener() {
	    public void onClick(View v) {
	    	Intent intent = new Intent(DetailActivity.this, SearchResultActivity.class);
	    	intent.putExtra("searchType", SearchType.INDEX_SEARCH.name());
			intent.putExtra("jamo", "ㅋ");
			startActivity(intent);
	    }
	};
	
	private OnClickListener mButton12ClickListener = new OnClickListener() {
	    public void onClick(View v) {
	    	Intent intent = new Intent(DetailActivity.this, SearchResultActivity.class);
	    	intent.putExtra("searchType", SearchType.INDEX_SEARCH.name());
			intent.putExtra("jamo", "ㅌ");
			startActivity(intent);
	    }
	};
	
	private OnClickListener mButton13ClickListener = new OnClickListener() {
	    public void onClick(View v) {
	    	Intent intent = new Intent(DetailActivity.this, SearchResultActivity.class);
	    	intent.putExtra("searchType", SearchType.INDEX_SEARCH.name());
			intent.putExtra("jamo", "ㅍ");
			startActivity(intent);
	    }
	};
	
	private OnClickListener mButton14ClickListener = new OnClickListener() {
	    public void onClick(View v) {
	    	Intent intent = new Intent(DetailActivity.this, SearchResultActivity.class);
	    	intent.putExtra("searchType", SearchType.INDEX_SEARCH.name());
			intent.putExtra("jamo", "ㅎ");
			startActivity(intent);
	    }
	};
	
	private OnClickListener mRangeButtonClickListener = new OnClickListener() {
	    public void onClick(View v) {
	    	Intent intent = new Intent(DetailActivity.this, SearchResultActivity.class);
	    	intent.putExtra("searchType", SearchType.RANGE_SEARCH.name());
			intent.putExtra("startText", startEditText.getText().toString());
			intent.putExtra("endText", endEditText.getText().toString());
			startActivity(intent);
	    }
	};
	
	private OnClickListener mJasoButtonClickListener = new OnClickListener() {
	    public void onClick(View v) {
	    	String[] jaso = new String[12];
	    	
	    	jaso[0] = edit01.getText().toString();
	    	jaso[1] = edit02.getText().toString();
	    	jaso[2] = edit03.getText().toString();
	    	jaso[3] = edit04.getText().toString();
	    	jaso[4] = edit05.getText().toString();
	    	jaso[5] = edit06.getText().toString();
	    	jaso[6] = edit07.getText().toString();
	    	jaso[7] = edit08.getText().toString();
	    	jaso[8] = edit09.getText().toString();
	    	jaso[9] = edit10.getText().toString();
	    	jaso[10] = edit11.getText().toString();
	    	jaso[11] = edit12.getText().toString();
	    	
	    	Intent intent = new Intent(DetailActivity.this, SearchResultActivity.class);
	    	intent.putExtra("searchType", SearchType.JASO_SEARCH.name());
			intent.putExtra("jaso", jaso);
			startActivity(intent);
	    }
	};
}

